require 'rho/rhocontroller'
require 'helpers/browser_helper'
require 'json'
require 'WebServices/base_service'

class TeacherController < Rho::RhoController
  include BrowserHelper

  def select_grade
    @grades = []
  	url = BaseService::SERVER_URL
    @grades = Rho::AsyncHttp.get(
	    :url => "#{url}/grades.json" 
    )['body']
    
    #puts "res: #{res}"
    #@grades = JSON.parse(res)

    render :action => :select_grade
  end

  def do_select_grade
  	$sign_up_info = $sign_up_info || {}
    $sign_up_info.merge!(@params)
    redirect :action => :select_subject
  end

  def select_subject
  	@subjects = []
  	url = BaseService::SERVER_URL
    @subjects = Rho::AsyncHttp.get(
	    :url => "#{url}/subjects.json" 
    )['body'] || []

    render :action => :select_subject
  end

  def do_select_subject
  	$sign_up_info = $sign_up_info || {}
    $sign_up_info.merge!(@params)

    url = BaseService::SERVER_URL
    param_string = {
      :user => {
        :user_name => $sign_up_info['username'],
        :password => $sign_up_info['password'],
        :role => $sign_up_info['type'],
        :name => $sign_up_info['name']          
      },
      :grades => $sign_up_info['grade'].keys,
      :subjects => $sign_up_info['subject'].keys      
    }.to_json
    Rho::AsyncHttp.post(
      :url => "#{url}/sign_up",
      :headers => {"content-type" => "application/json"},
      :body => param_string 
    )['body'] || []
    redirect :controller => :Session, :action => :login  	
  end
  
  def questions
    #Need to filter on Rhoconnect server
    response = Rho::AsyncHttp.get(
      :url =>  WEBSERVER_URL + "get_teacher_questions",
      :body => "user_id=#{$user['id']}" 
    )
    
    @questions = response["body"]
      
    render :action => :questions
    #Remove question that already in My Question
    
  end
  
  
  
  def new_questions
    response = Rho::AsyncHttp.get(
          :url =>  WEBSERVER_URL + "get_new_questions",
          :body => "user_id=#{$user['id']}" 
    )
    
    @questions = response["body"]["data"]
    
    render :action => :new_questions
  end
  
  def profile
      @profile = $user
      render :action => :profile
    end
    
  def withdraw_money
    @profile = $user
            
    render :action => :withdraw_money
  end
  
  def post_withdraw_money
      @transfer_code = @params['transfer_code']
      @user_id = $user['id']
      @amount = @params['amount']
        
      url = "http://10.10.10.154:3000/withdraw"
      param_string = {:user_id => @user_id, :transfer_code => @transfer_code, :amount => @amount}.to_json
      
          response = Rho::AsyncHttp.post(
          :url => url,
          :headers => {"content-type" => "application/json"},
          :body => param_string
          )
      
          total_point = response["body"]["point"].to_i
            
          $user['point'] = total_point.to_s
            
          redirect url_for(:action => :profile, :controller => :Teacher)
    end
  
  def index
    @profile = $user

    @number_of_questions = 0

    @user_id = $user['id']

    url = "http://10.10.10.154:3000/unanswered_question_count?user_id=#{@user_id}"

    response = Rho::AsyncHttp.get(
    :url => url
    )

    @number_of_questions = response["body"]["count"].to_i

    render :action => :index
  end
end
